/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AplManager.controller;
import AplManager.views.*;
import AplManager.models.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
 *
 * @author user
 */
public class EditPlayerController {
     private EditPlayer view;
      private TableDisplay tableDisplayView;
     private Team model;
     private int numToBeChngd;
    private ActionListener actionListener;
    
    public EditPlayerController(Team model,EditPlayer view,TableDisplay tableDisplayView, int num){
       this.model = model;
        this.view  = view;
         this.tableDisplayView  = tableDisplayView;
         numToBeChngd = num;
                     
    }
    
    public void control(){        
        actionListener = new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) { 
              if(e.getSource().equals(view.getSaveButton())){
                     String fname = view.getFirstName();
                     String lname = view.getLastName();
                     int num = view.getNumber();
                     String position = view.getPosition();
                     int goals = view.getGoals();
                     int  assists = view.getAssists();
                     Player player = new Player(fname,lname,position,num);
                     player.setGoals(goals);
                     System.out.println(player.getGoals());
                     player.setAssists(assists);
                     System.out.println("I got this far");
                     model.updatePlayer(numToBeChngd,player);
                     System.out.println(numToBeChngd);
                     System.out.println(model.getCurSize());
                     //tableDisplayView.resetTable();
                     //for(int i=0; i <model.getCurSize();i++){
                     Object[] row = {model.getPlayers()[numToBeChngd].getFirstName(),model.getPlayers()[numToBeChngd].getlastName(),
                                model.getPlayers()[numToBeChngd].getNumber()+"",model.getPlayers()[numToBeChngd].getPosition(),
                                 model.getPlayers()[numToBeChngd].getGoals()+"", model.getPlayers()[numToBeChngd].getAssists()+"",
                                 model.getPlayers()[numToBeChngd].isInStartingLineUp()};
                     tableDisplayView.insertRows(numToBeChngd, row);
                     
                    // }
                      view.setVisible(false);
                  }
              }
        };               
        view.getSaveButton().addActionListener(actionListener);
    }
}
